package com.yeban.bams.entity;

import com.yeban.bams.exception.LoanException;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/10/13
 *
 * 3 - 可贷款信用账户
 * 可以贷款,可以透支
 */
public class LoanableCreditAccount extends CreditAccount implements Loanable{

    private long loanAmount;    // 贷款金额
    private static final long serialVersionUID = 7010709030704620974L;

    public LoanableCreditAccount() {
    }

    public LoanableCreditAccount(Long id, String password, String name, String personId, String email, double balance, double ceiling, long loanAmount) {
        super(id, password, name, personId, email, balance, ceiling);
        this.loanAmount = loanAmount;
        this.setType(3);
    }

    /**
     * 用户取款
     * @param money 取款金额
     * @return
     */
    @Override
    public LoanableCreditAccount withdraw(double money) {
        double balance = getBalance();
        double ceiling = getCeiling();
        double available = balance + ceiling;
        if(money <= 0) {
            System.out.println("取款金额必须为正数");
            return this;
        }
        if(money > available) {
            System.out.println("取款失败！超过余额+透支额度");
            return this;
        }
        if(money <= balance) {
            setBalance(balance - money);
            return this;
        }
        double overMoney = money - balance; // 需要用透支额度
        setBalance(0);
        setCeiling(ceiling - overMoney);
        System.out.println("当前余额为：0.0当前透支额度为：" + getCeiling());
        return this;
    }

    public long getLoanAmount() {
        return loanAmount;
    }

    public void setLoanAmount(long loanAmount) {
        this.loanAmount = loanAmount;
    }

    /**
     * 贷款
     * @param money 贷款金额
     * @return
     */
    @Override
    public Account requestLoan(double money) throws LoanException {
        // 贷款金额money < 0
        if(money < 0.0) {
            throw new LoanException("贷款金额必须为正数！");
        }
        // 贷款逻辑
        loanAmount += money;                            // 设置贷款额
        this.setBalance(this.getBalance() + money);     // 贷款的钱到账户上
        return this;
    }

    /**
     * 还款
     * @param money 还款金额
     * @return
     */
    @Override
    public Account payLoan(double money) throws LoanException {
        double balance = getBalance();
        double ceiling = getCeiling();
        double available = balance + ceiling;
        // 还款金额money < 0
        if(money < 0.0) {
            throw new LoanException("还款金额必须为正数！");
        }

        // 还款金额money > 贷款额loanAmount
        if(money > loanAmount) {
            throw new LoanException("还款金额超出贷款额");
        }

        // 还款逻辑
        if(money <= balance) {                  // 还款 <= 余额
            setBalance(balance - money);
            loanAmount -= money;
            return this;
        } else if(money <= balance + ceiling){  // 还款 <= 余额 + 透支额度
            setBalance(0);
            setCeiling(ceiling - (money - balance));
            loanAmount -= money;
            return this;
        } else {                                // 还款 > 余额 + 透支额度
            throw new LoanException("余额不足！还款失败");
        }
    }

    @Override
    public String toString() {
        return super.toString() + "LoanableCreditAccount{" +
                "loanAmount=" + loanAmount +
                '}' + super.toString();
    }
}
